package com.zgroup.domain

class Bid {
  static Map CURRENCY_UNIT = ["10":"分", "20":"点","30":"元"];
  static Map TIME_UNIT = ["10": "天", "20": "月"]

  String type = "10"          // 10:竞标 20:vip
  String payment = "10"       // 支付方式 10:积分 20:点券 30：现金 40:赠送
  String status = "10"        // 10:竞标 20:被挤掉 30:主动取消
  String expired = "0"        // 过期标记 0:没有过期 1:过期
  BigDecimal money          // 金额
  BigDecimal dayCost        // 每日单价
  BigDecimal totalCost = BigDecimal.ZERO     // 总消费
  Integer times = 0           // 次数

  Estate estate
  User user

  Date dateCancelled        // 取消/被挤掉竞标的时间
  Date dateCreated

  long getCreateTime() {
    return (new Date().getTime() - dateCreated?.getTime()) / 1000
  }

  long getCancelledTime() {
    return (new Date().getTime() - dateCancelled?.getTime()) / 1000
  }

  boolean getIsVipType() {
    return "20" == type
  }

  boolean getIsFree() {
    def setting = Setting.get(1)
    def freeTime = 3600 * setting.freeBidHours
    return (new Date().getTime() - dateCreated?.getTime()) / 1000 < freeTime
  }

  String getDisplayCost() {
    if ("30" == payment) return money
    return money.toBigInteger()
  }

  String getDisplayUnit() {
    return CURRENCY_UNIT[payment] + "/" + TIME_UNIT[type]
  }

  static transients = ["createTime", "cancelledTime", "isFree", "isVipType", "displayCost", "displayUnit"]
  //static belongsTo =[estate:Estate,user:User]

  def beforeInsert = {
    totalCost = money * times
  }

  def beforeUpdate = {
    totalCost = money * times
  }

  static constraints = {
    money(blank: false, range: 0..9999999999, scale: 2)
    dayCost(blank: false, range: 0..9999999999, scale: 2)
    totalCost(blank: false, range: 0..99999999999, scale: 2)
    type(blank: false, maxSize: 6)
    payment(blank: false, maxSize: 6)
    status(blank: false, maxSize: 6)
    expired(blank: false, maxSize: 6)
    dateCancelled(nullable: true)
  }

  static mapping = {
    dynamicInsert true
    dynamicUpdate true
    order "desc"
  }

  String toString() {
    "id:" + id + ",type:" + type + ",payment:" + payment + ",status:" + status + ",money:" + money + ",dayCost:" + dayCost
  }
}
